Option Strict Off
Option Explicit On
Module SteveOsStandardFunctions
	
	'MyReplace works even if the LookFor is not in the LookIn...
	' the normal VB Replace function errors in this condition
	Public Function MyReplace(ByRef LookIn As String, ByRef LookFor As String, ByRef ReplaceWith As String) As String
		If InStr(1, LookIn, LookFor, CompareMethod.Binary) > 0 Then
			LookIn = Replace(LookIn, LookFor, ReplaceWith)
		End If
		MyReplace = LookIn
	End Function
	
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function MyCustomTrim(ByRef InputString_Renamed As String, ByRef TrimChars As String) As String
		If InStr(1, InputString_Renamed, TrimChars) = 0 Then
			MyCustomTrim = InputString_Renamed
			Exit Function
		End If
		
		If Left(InputString_Renamed, Len(TrimChars)) = TrimChars And Right(InputString_Renamed, Len(TrimChars)) = TrimChars Then
			MyCustomTrim = Mid(InputString_Renamed, Len(TrimChars) + 1, Len(InputString_Renamed) - (Len(TrimChars) * 2))
		Else
			MyCustomTrim = InputString_Renamed
		End If
	End Function
	
	
	'AutoPeriod adds a period to the end of a string if it's not there...
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function AutoPeriod(ByRef InputString_Renamed As String) As String
		If Right(InputString_Renamed, 1) <> "." Then
			AutoPeriod = RTrim(InputString_Renamed) & "."
		Else
			AutoPeriod = InputString_Renamed
		End If
	End Function
	
	'Returns true if the input string is an IP Address.  ex: 192.168.1.1
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function IsIPAddress(ByRef InputString_Renamed As String) As Boolean
		IsIPAddress = False
		If Len(InputString_Renamed) < 7 Or Len(InputString_Renamed) > 15 Then
			Exit Function
		End If
		If InStr(1, InputString_Renamed, ".", CompareMethod.Binary) = 0 Then
			Exit Function
		End If
		Dim aryOctets() As String
		aryOctets = Split(InputString_Renamed, ".")
		If UBound(aryOctets) <> 3 Then
			Exit Function
		End If
		Dim lngIndex As Integer
		For lngIndex = 0 To UBound(aryOctets)
			If Not IsNumeric(aryOctets(lngIndex)) Then
				Exit Function
			End If
			If CInt(aryOctets(lngIndex)) < 0 Or CInt(aryOctets(lngIndex)) > 255 Then
				Exit Function
			End If
		Next 
		IsIPAddress = True
	End Function
	
	'Same as ltrim, but kills tabs and nulls as well...
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function MyLTrim(ByVal InputString_Renamed As String) As String
		If Len(InputString_Renamed) = 0 Then
			MyLTrim = ""
			Exit Function
		End If
		
		Dim flgNonWhitespaceFound As Boolean
		flgNonWhitespaceFound = False
		While Len(InputString_Renamed) > 0 And flgNonWhitespaceFound = False
			If StrComp(Left(InputString_Renamed, 1), " ", CompareMethod.Binary) = 0 Or StrComp(Left(InputString_Renamed, 1), Chr(9), CompareMethod.Binary) = 0 Or StrComp(Left(InputString_Renamed, 1), Chr(0), CompareMethod.Binary) = 0 Then
				InputString_Renamed = Mid(InputString_Renamed, 2) 'cuts leftmost character...
			Else
				flgNonWhitespaceFound = True 'Non whitespace or null character... done trimming this side.
			End If
		End While
		MyLTrim = InputString_Renamed
	End Function
	
	'Same as RTrim, but kills tabs and nulls as well...
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function MyRTrim(ByVal InputString_Renamed As String) As String
		If Len(InputString_Renamed) = 0 Then
			MyRTrim = ""
			Exit Function
		End If
		
		Dim flgNonWhitespaceFound As Boolean
		flgNonWhitespaceFound = False
		While Len(InputString_Renamed) > 0 And flgNonWhitespaceFound = False
			If StrComp(Right(InputString_Renamed, 1), " ", CompareMethod.Binary) = 0 Or StrComp(Right(InputString_Renamed, 1), Chr(9), CompareMethod.Binary) = 0 Or StrComp(Right(InputString_Renamed, 1), Chr(0), CompareMethod.Binary) = 0 Then
				InputString_Renamed = Mid(InputString_Renamed, 1, Len(InputString_Renamed) - 1) 'cuts rightmost character...
			Else
				flgNonWhitespaceFound = True 'Non whitespace or null character... done trimming this side.
			End If
		End While
		MyRTrim = InputString_Renamed
	End Function
	
	'UPGRADE_NOTE: InputString was upgraded to InputString_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function MyTrim(ByRef InputString_Renamed As String) As String
		MyTrim = MyLTrim(MyRTrim(InputString_Renamed))
	End Function
	
	Public Function WMIErrorHelp(ByRef ErrorNumber As Integer) As String
		Select Case ErrorNumber
			Case -2147024891
				WMIErrorHelp = "Access Denied"
			Case -2147023174
				WMIErrorHelp = "Computer Not Found"
			Case Else
				WMIErrorHelp = ""
		End Select
	End Function
	
	'Counts the number of times one string occurs inside another...
	Public Function OccurrenceCount(ByRef LookIn As String, ByRef LookFor As String) As Integer
		
		Dim lngIndex, lngCount As Integer
		
		lngIndex = 1
		lngCount = 0
		While InStr(lngIndex, LookIn, LookFor, CompareMethod.Binary) > 0
			lngCount = lngCount + 1
			lngIndex = InStr(lngIndex, LookIn, LookFor, CompareMethod.Binary) + Len(LookFor)
		End While
		
		OccurrenceCount = lngCount
	End Function
End Module